package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.LOCALMAC;
import com.thinkgem.jeesite.common.utils.RSAUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.LicenseKey;
import com.thinkgem.jeesite.modules.sys.service.LicenseService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author John
 * @Date 2016/4/19
 */
@Controller
@RequestMapping(value = "${adminPath}/license")
public class LicenseController extends BaseController{

    @Autowired
    private LicenseService LicenseService;

    /**
     * 获取随机码
     * @return
     */
    @RequestMapping(value = "buildLicenseId")
    @ResponseBody
    public String buildLicenseId(){
        LicenseKey LicenseKey = LicenseService.get("");
        String licenseId;
        if(null == LicenseKey || StringUtils.isBlank(LicenseKey.getLicenseId())){
            licenseId = RSAUtils.getRandomStringZM(20);
            licenseId += LOCALMAC.getLocalOneMac();

            LicenseService.insertId(licenseId);
            if(null != LicenseKey){
                LicenseKey.setLicenseId(licenseId);
            }else{
                LicenseKey = new LicenseKey();
                LicenseKey.setLicenseId(licenseId);
            }
        }else{
            licenseId = LicenseKey.getLicenseId();
        }

        Global.setLicense(LicenseKey);
        return licenseId;
    }
    /**
     * 获取随机码
     * @return
     */
    @RequestMapping(value = "reBuildLicenseId")
    @ResponseBody
    public String reBuildLicenseId(){
        String licenseId = RSAUtils.getRandomStringZM(20);
        licenseId += LOCALMAC.getLocalOneMac();

        LicenseService.delete(null);

        LicenseService.insertId(licenseId);

        LicenseKey LicenseKey = new LicenseKey(licenseId, null);
        Global.setLicense(LicenseKey);

        return licenseId;
    }

    /**
     * 验证授权文件
     */
    @RequestMapping(value = "checkLicense", method = RequestMethod.POST)
    public String license(Model model, MultipartFile mfile) throws IOException{
        LicenseKey licenseKey = Global.getLicense();
        try{
            CommonsMultipartFile cf = (CommonsMultipartFile)mfile;
            DiskFileItem fi = (DiskFileItem)cf.getFileItem();
            InputStream inputStream = fi.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "GB2312"));
            StringBuilder license = new StringBuilder();
            String line;
            while((line = br.readLine()) != null){
                license.append(line.trim());
            }

            Result result = RSAUtils.validateLicense(license.toString());
            if(Result.SUCCESS.equals(result.getCode())){
                LicenseService.update(licenseKey);
                return "redirect:" + adminPath + "/login";
            }
            else{
                model.addAttribute("licenseId", licenseKey.getLicenseId());
                model.addAttribute("message", result.getMsg());
                return "modules/sys/license";
            }
        }
        catch(Exception e){
            model.addAttribute("licenseId", licenseKey.getLicenseId());
            model.addAttribute("message", "注册码错误，请联系管理员！");
            return "modules/sys/license";
        }
    }
}
